package admin.controllers;

import helpers.IdUniqueHelper;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.TreeMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import models.DealModel;
import models.ProductModel;
import models.StoreItemModel;
import models.TransactionModel;
import admin.data.DealData;
import admin.data.IdCountData;
import admin.data.ProductData;
import admin.data.StoreData;
import admin.data.StoreItemData;
import admin.data.TransactionData;

@SuppressWarnings("serial")
public class AddStoreItemServlet extends HttpServlet {
	@SuppressWarnings("rawtypes")
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		response.setCharacterEncoding("UTF-8");

		String path = ((HttpServletRequest) request).getRequestURI();
		String params = path.replaceAll("/admin/store-item/add/", "");
		
		if(params.length()>3)
		{
			StoreItemData storeItemData = new StoreItemData();
			StoreItemModel storeItemModel = storeItemData.getItem(params);
			if(storeItemModel.getId().length()>3)
			{
				request.setAttribute("dealId", storeItemModel.getDealId());
				request.setAttribute("storeItemModel", storeItemModel);
			}
		}
		else
		{ 
			request.setAttribute("dealId", null);
		}

		try {
			request.getRequestDispatcher("/admin/pages/add_store_item.jsp")
					.forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		}
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		response.setCharacterEncoding("UTF-8");

		String action = (String) request.getParameter("action");

		if(action.equalsIgnoreCase("search_deal"))
		{
			String dealId = (String) request.getParameter("dealId");
			DealModel dealModel = DealData.getById(dealId);
			if(dealModel==null) { 
				request.setAttribute("dealId", null);
			}
			else {			
				List<ProductModel> listProduct = ProductData.getByDealId(dealModel.getDealId());
				request.setAttribute("listProduct", listProduct);
				request.setAttribute("dealId", dealModel.getDealId());
				request.setAttribute("dealModel", dealModel);
			}
			try {
				request.getRequestDispatcher("/admin/pages/add_store_item.jsp")
						.forward(request, response);
			} catch (ServletException e) {
				e.printStackTrace();
			}
		}
		else if(action.equalsIgnoreCase("add_new_store_item"))
		{
			String storeId = (String) request.getParameter("storeId");
			String productId = (String) request.getParameter("productId");
			String type = (String) request.getParameter("type");
			
			StoreItemData storeItemData = new StoreItemData();
			StoreItemModel storeItem = storeItemData.getItem(productId);
			if(storeItem.getAmount()>0)
			{
				HttpSession session = request.getSession();
				session.setAttribute("alert", "Sản phẩm đã tồn tại, hãy cập nhật thêm sản phẩm vào kho !");
				response.sendRedirect("/admin/store-item/add/"+storeItem.getId());
			}
			else
			{
				int amount = 0;
				try { amount = Integer.parseInt(request.getParameter("amount")); } catch (Exception e) { amount = 0; }
				
				if (amount>0) {
					StoreItemModel storeItemModel = new StoreItemModel();
					
					ProductModel productModel = ProductData.getById(productId);
					storeItemModel.setStoreId(storeId);
					storeItemModel.setDealId(productModel.getDealId());
					storeItemModel.setProductId(productModel.getProductId());
					storeItemModel.setDealName(productModel.getDealName());
					storeItemModel.setProductName(productModel.getName());
					storeItemModel.setType(Integer.parseInt(type));
	
					storeItemModel.setId(productModel.getProductId());
	
					storeItemModel.setAmount(amount);
					storeItemModel.setInventory(amount);
					
					storeItemData.insert(storeItemModel);
					
					HttpSession session = request.getSession();
					session.setAttribute("success", "Thêm mới sản phẩm thành công !");
					
					TransactionModel transaction = new TransactionModel();
					transaction.setTransactionId(IdUniqueHelper.getId());
					transaction.setDealId(storeItemModel.getDealId());
					transaction.setStoreItemId(storeItemModel.getId());
					transaction.setInventory(storeItemModel.getInventory());
					transaction.setDelivered(storeItemModel.getDelivered());
					transaction.setDelivery(storeItemModel.getDelivery());
					transaction.setLoc(storeItemModel.getLoc());
					transaction.setType(1);
					transaction.setDate(new Date());
					transaction.setAmount(amount);
					TransactionData transactionData = new TransactionData();
					transactionData.insert(transaction);		
					
					response.sendRedirect("/admin/store-item/list/"+storeItemModel.getId());
				}		
			}
		}
		else if(action.equalsIgnoreCase("add_old_store_item"))
		{
			String productId = (String) request.getParameter("productId");
			int amount = 0;
			try { amount = Integer.parseInt(request.getParameter("amount")); } catch (Exception e) { amount = 0; }
			
			if (amount>0) {
				StoreItemData storeItemData = new StoreItemData();
				StoreItemModel storeItemModel = storeItemData.getItem(productId);
				if(storeItemModel.getId().length()>3)
				{
					storeItemModel.setAmount(amount + storeItemModel.getAmount());
					storeItemModel.setInventory(amount + storeItemModel.getInventory());
					storeItemData.insert(storeItemModel);
					
					HttpSession session = request.getSession();
					session.setAttribute("success", "Nhập thêm sản phẩm thành công !");

					TransactionModel transaction = new TransactionModel();
					transaction.setTransactionId(IdUniqueHelper.getId());
					transaction.setDealId(storeItemModel.getDealId());
					transaction.setStoreItemId(storeItemModel.getId());
					transaction.setInventory(storeItemModel.getInventory());
					transaction.setDelivered(storeItemModel.getDelivered());
					transaction.setDelivery(storeItemModel.getDelivery());
					transaction.setLoc(storeItemModel.getLoc());
					transaction.setType(1);
					transaction.setDate(new Date());
					transaction.setAmount(amount);
					TransactionData transactionData = new TransactionData();
					transactionData.insert(transaction);
					
					response.sendRedirect("/admin/store-item/list/"+storeItemModel.getId());
				}
			}
		}
	}
}